// pages/hear/hear.js
const config = require('../../config.js')
const http = require('../../http.js')
const innerAudioContext = wx.createInnerAudioContext()
var curIndex = 0
var length = 0
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPause: false,
    itemId: -1,
    index: 0,
    lawData: [],
    countLike: 0,
    tmpfile: {
      tempFilePath: '',
      duration: 0,
      fileSize: 0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options", options)
    curIndex = 0
    if (options.itemId) {
      let id = parseInt(options.itemId)
      this.setData({
        itemId: id
      })
    }
    if (options.roomId) {
      this.getMeetingLaw(options.roomId)
      this.setData({
        roomId: options.roomId
      })
    }

    this.initInnerAudioContext()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.stopVoice()
    // innerAudioContext.destroy()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.stopVoice()
    // innerAudioContext.destroy()
  },

  initInnerAudioContext: function () {
    innerAudioContext.onPlay((res) => {
      console.log("onPlay", res)
    })
    innerAudioContext.onPause((res) => {
      console.log("onPause", res)
    })
    innerAudioContext.onError((res) => {
      console.log("onError", res)
      this.setData({
        showPause: false
      })
    })
    innerAudioContext.onSeeked((res) => {
      console.log("onSeeked", res)
    })
    innerAudioContext.onStop((res) => {
      console.log("onStop", res)
    })
    innerAudioContext.onWaiting((res) => {
      console.log("onWaiting", res)
    })
    innerAudioContext.onEnded((res) => {
      console.log("onEnded", res)
      this.setData({
        showPause: false
      })
    })
  },

  /**
   * 播放
   */
  playVoice: function () {
    this.pauseVoice()
    this.setData({
      countLike: this.data.lawData[curIndex].countLike
    })
    if (this.data.lawData[curIndex].tag) {
      innerAudioContext.src = this.data.lawData[curIndex].resUrl
      innerAudioContext.play()
      console.log("播放")
      this.setData({
        showPause: true
      })
      for (var i = 0; i < length; i++) {
        if (i == curIndex) {
          this.data.lawData[i].isPlay = true
        } else {
          this.data.lawData[i].isPlay = false
        }
      }
    } else {
      wx.showToast({
        title: '当前条目没有音频资源',
        icon: 'none'
      })

      for (var i = 0; i < length; i++) {
        this.data.lawData[i].isPlay = false
      }
    }

    this.setData({
      lawData: this.data.lawData,
      index: curIndex
    })

  },
  /**
   * 暂停
   */
  pauseVoice: function () {
    innerAudioContext.pause()
    console.log("暂停")
    this.setData({
      showPause: false
    })
  },
  /**
   * 继续
   */
  resumeVoice: function () {
    innerAudioContext.play()
    console.log("继续")
  },
  /**
   * 停止
   */
  stopVoice: function () {
    innerAudioContext.stop()
  },

  lastVoice: function () {
    console.log("上一个")
    curIndex--
    if (curIndex < 0) {
      curIndex = 0
      wx.showToast({
        title: '当前是第一条',
        icon: 'none'
      })
      return
    }
    this.playVoice()

  },
  nextVoice: function () {
    console.log("下一个")
    curIndex++
    if (curIndex >= length) {
      curIndex = length - 1
      wx.showToast({
        title: '当前是最后一条',
        icon: 'none'
      })
      return
    }
    this.playVoice()
  },
  favorite: function () {
    console.log("点赞")
    var that = this
    http.request(
      config.meetings + "/" + this.data.roomId + "/like/" + this.data.lawData[curIndex].id,
      'POST', null,
      function (res) {
        if (res.data != null) {
          that.data.lawData[curIndex].countLike = res.data
          that.setData({
            countLike: res.data
          })
        }
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }
    )
  },


  getMeetingLaw: function (rId) {
    var that = this
    http.request(
      config.meetings + "/" + rId,
      'POST', null,
      function (res) {
        if (res.data != null && res.data.length > 0) {
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].resUrl != null && res.data[i].resUrl != "") {
              res.data[i].tag = true
            } else {
              res.data[i].tag = false
            }
            res.data[i].isPlay = false

            if (that.data.itemId > -1 && res.data[i].id == that.data.itemId) {
              curIndex = i
            }
          }
          that.setData({
            lawData: res.data,
            index: curIndex,
            countLike: res.data[curIndex].countLike
          })
          length = res.data.length

          if (that.data.itemId > -1) {
            that.playVoice()
          }
        }
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }
    )
  }
})